package com.jsmy.marketing.fg;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.jsmy.marketing.R;
import com.jsmy.marketing.adapter.ComFragmentAdapter;
import com.jsmy.marketing.adapter.DataFragmentAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.bean.TXLBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/17.
 * Time: 17:48.
 * To change for file | settings | file and code templates.
 */

public class DataFragment extends BaseFragment {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.m_viewpage)
    ViewPager mViewpage;
    @BindView(R.id.m_tab)
    TabLayout mTab;
    ComFragmentAdapter adapter;
    TXLBean.DataBean dataBean;
    DataFragmentAdapter fragmentAdapter;
    @Override
    protected int resLayout() {
        return R.layout.fg_data_layout;
    }

    @Override
    protected void initData() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                requestData();
            }
        });
        refreshLayout.setEnableLoadmore(false);
        mTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        requestData();
    }

    private void requestData() {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        mPresenter.loadData(RetrofitManager.getInstance().gettxllist(map), Link.APP_TXL_LIST);
    }
    private List<Fragment> fragmentList;
    @Override
    public void refreshData(Object object, String position) {
        switch (position) {
            case Link.APP_TXL_LIST:
                if (((TXLBean) object).getCode().equals("Y") ){
                    dataBean = ((TXLBean) object).getData();

                    if (fragmentAdapter==null){
                        fragmentList=new ArrayList<>();
                        for (int i=0;i<dataBean.getList().size();i++){
                            DataItemFragment dataItemFragment;
                            Bundle b = new Bundle();
                            b.putSerializable("dataItemList", (Serializable) dataBean.getList().get(i).getList());
                            dataItemFragment = new DataItemFragment();
                            dataItemFragment.setArguments(b);
                            fragmentList.add(dataItemFragment);
                        }
                        fragmentAdapter=new DataFragmentAdapter(getChildFragmentManager(),fragmentList){
                            @Nullable
                            @Override
                            public CharSequence getPageTitle(int position) {
                                return dataBean.getList().get(position).getTitle();
                            }
                        };
                        mViewpage.setAdapter(fragmentAdapter);
                    mViewpage.setOffscreenPageLimit(3);
                    mTab.setupWithViewPager(mViewpage);
                    }else {
                        for (int i=0;i<dataBean.getList().size();i++){
                            fragmentAdapter.getItem(i).updateData(dataBean.getList().get(i).getList());
                        }

                    }
//                if (adapter == null) {
//                    adapter = new ComFragmentAdapter(getChildFragmentManager(), dataBean.getList().size()) {
//                        DataItemFragment dataItemFragment;
//                        @Override
//                        public Fragment getItem(int postion) {
//                            Bundle b = new Bundle();
//                            b.putSerializable("dataItemList", (Serializable) dataBean.getList().get(postion).getList());
//                            dataItemFragment = new DataItemFragment();
//                            dataItemFragment.setArguments(b);
//                            return dataItemFragment;
//                        }
//
//                        @Override
//                        public CharSequence getPageTitle(int position) {
//                            return dataBean.getList().get(position).getTitle();
//                        }
//                    };
//                    mViewpage.setAdapter(adapter);
//                    mViewpage.setOffscreenPageLimit(3);
//                    mTab.setupWithViewPager(mViewpage);
//                    adapter.notifyDataSetChanged();
//                }else {
//                    mViewpage.removeAllViews();
//                    mViewpage.removeAllViewsInLayout();
//                    mViewpage.destroyDrawingCache();
//                    adapter = new ComFragmentAdapter(getChildFragmentManager(), dataBean.getList().size()) {
//                        DataItemFragment dataItemFragment;
//                        @Override
//                        public Fragment getItem(int postion) {
//                            Bundle b = new Bundle();
//                            b.putSerializable("dataItemList", (Serializable) dataBean.getList().get(postion).getList());
//                            dataItemFragment = new DataItemFragment();
//                            dataItemFragment.setArguments(b);
//                            return dataItemFragment;
//                        }
//
//                        @Override
//                        public CharSequence getPageTitle(int position) {
//                            return dataBean.getList().get(position).getTitle();
//                        }
//                    };
//                    mViewpage.setAdapter(adapter);
//                    mViewpage.setOffscreenPageLimit(3);
//                    mTab.setupWithViewPager(mViewpage);
//                }
                }else {
                    ToastUtil.showShort(getContext(),((TXLBean) object).getMsg());
                }
                 if (refreshLayout.isRefreshing()){
                     refreshLayout.finishRefresh();
                 }
                break;
        }
    }
    public void update(){
        refreshLayout.autoRefresh();
    }

    public interface OnDataFGListener{
        void updateData(List<TXLBean.DataBean.ListBeanX.ListBean> mList);
    }
}
